<script setup lang="ts">
import { closeToast, showLoadingToast, type ActionSheetAction } from 'vant'
import { ref, onMounted } from 'vue'
import { http } from '@/utils/http'

// 获取用户数据
const profile = ref<ProfileInfo>({} as ProfileInfo)
onMounted(async () => {
  showLoadingToast({
    message: '加载中...',
    duration: 0,
    forbidClick: true,
  })
  const res = await http.get<MkResponse<ProfileInfo>>('/member/profile')
  profile.value = res.data.result
  closeToast()
  console.log(profile.value)
})

// 头像处理
const actions = [{ name: '拍照' }, { name: '相册' }]
const showAvatarSheet = ref(false)
const onSelect = (action: ActionSheetAction, index: number) => {
  showAvatarSheet.value = false
  if (index === 0) {
    // 拍照
  } else if (index === 1) {
  }
}

// 出生日期、
const showBirthdayPopup = ref(false)
const onChangeBirthday = () => {
  showBirthdayPopup.value = false
}

// 省市区
interface Location {
  selectedValues: string[]
}
const showLocationPopup = ref(false)
const areaColumns = ref<AreaColumns>({} as AreaColumns)
onMounted(async () => {
  const res = await mk.getAreaColumns()
  areaColumns.value = res
})
const onSelectedLocation = (obj: Location) => {
  console.log(obj.selectedValues)
  showLocationPopup.value = false
}
</script>

<template>
  <div class="profile-edit-page">
    <div class="avatar">
      <van-image
        round
        width="100"
        height="100"
        class="avatar=img"
        :src="profile.avatar"
        @click="showAvatarSheet = true"
      >
      </van-image>
      <div class="avatar-btn">
        <span>修改头像</span>
      </div>
    </div>
    <van-action-sheet
      cancel-text="取消"
      v-model:show="showAvatarSheet"
      :actions="actions"
      @select="onSelect"
      @cancel="showAvatarSheet = false"
      style="padding-bottom: 40px"
    />
    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-field v-model="profile.account" label="账号" readonly> </van-field>
      <van-field v-model="profile.nickname" label="昵称" placeholder="请输入昵称"> </van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group v-model="profile.gender" :icon-size="16">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <van-field
        v-model="profile.birthday"
        label="生日"
        readonly
        placeholder="请选择日期"
        @click="showBirthdayPopup = true"
      >
      </van-field>
      <van-popup v-model:show="showBirthdayPopup" position="bottom">
        <van-date-picker
          title="选择日期"
          @cancel="showBirthdayPopup = false"
          @confirm="onChangeBirthday"
        />
      </van-popup>

      <van-field
        v-model="profile.fullLocation"
        label="所在地"
        readonly
        placeholder="请选择所在地"
        @click="showLocationPopup = true"
      ></van-field>
      <van-popup position="bottom" v-model:show="showLocationPopup">
        <van-area
          title="选择省市区"
          :area-list="areaColumns"
          @cancel="showLocationPopup = false"
          @confirm="onSelectedLocation"
        />
      </van-popup>
      <van-field
        v-model="profile.profession"
        label="职业"
        readonly
        placeholder="请选择职业"
      ></van-field>
    </van-cell-group>
    <div class="submit">
      <van-button round block type="primary">保存资料</van-button>
    </div>
  </div>
</template>

<style lang="css" scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px var(--mk-gray);
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>
